﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using BTv2.Model;
using BTv2.UserControls;

namespace BTv2.Controls
{
   public class NotificationsController
    {
       public static string NormalNotificationType = "Normal";
       public static string CryticalNotificationType = "Crytical";
       private static NotificationsController _instance;

       private MainWindowDefaultState _nootificationDisplayer;
       private List<Notification> _notifications = new List<Notification>();
       public static NotificationsController GetInstance()
       {
           if (_instance == null) _instance = new NotificationsController();
           return _instance;
       }


       public void SetNotificationDisplayer(MainWindowDefaultState mwdws)
       {
           _nootificationDisplayer = mwdws;
       }

       public async void PreloadNotfications()
       {
           //fetch from DB last 10 

           //check notifications if send

           // bind to list
       }
       public async void AddNotification(string NotificationText, string NotificationType)
       {

           Notification n  = new Notification(){Content = NotificationText, Timestamp= DateTime.Now, Type = NotificationType, Executed =false};
           try
           {
               Application.Current.Dispatcher.Invoke(() => _nootificationDisplayer.AddNotification(n));
               n.Executed = true;
               _notifications.Add(n);
           }
           catch
           {
               _notifications.Add(n);
           }
       }
     

       public List<Notification> FetchLastFour()
       {
           if (_notifications == null) return new List<Notification>();
           return _notifications.OrderByDescending(x => x.Timestamp).Take(4).ToList();
       }

    }
}
